#include "03_header.h"

int sendTrain(int sockfd, int type, const char *value) {
    train_t train;
    train.type = type;
    train.lenth = strlen(value);
    memset(train.value, 0, sizeof(train.value));

    if (value) {
        strncpy(train.value, value, sizeof(train.value) - 1);
    }

    if (send(sockfd, &train, sizeof(int) * 2 + train.lenth, 0) <= 0) {
        perror("发送数据失败");
        return -1;
    }

    return 0;
}

int receiveTrain(int sockfd, train_t *train) {
    bzero(train, sizeof(train_t));
//    int total = 0;
//    int ret = 0;
//    char *buf = (char *)&train->type;
//    while (total < (int)sizeof(int)) {
//        ret = recv(sockfd, buf + total, sizeof(int) - total, 0);
//        if (ret <= 0) {
//            fprintf(stderr, "recv type failed: %d\n", ret);
//            return -1;
//        }
//        total += ret;
//    }
//    train->type = ntohl(train->type); // 转换为主机字节序
//    printf("Received type: %d\n", train->type);
//
//    total = 0;
//    buf = (char *)&train->lenth;
//    while (total < (int)sizeof(int)) {
//        ret = recv(sockfd, buf + total, sizeof(int) - total, 0);
//        if (ret <= 0) {
//            fprintf(stderr, "recv lenth failed: %d\n", ret);
//            return -1;
//        }
//        total += ret;
//    }
//    //train->lenth = ntohl(train->lenth); // 转换为主机字节序
//    printf("Received lenth: %d\n", train->lenth);
//    if (recv(sockfd, train, sizeof(int) * 2, 0) <= 0) {
//        perror("接收头部失败");
//        return -1;
//    }
//
//    if (train->lenth > 0) {
//        if (train->lenth >= (int)sizeof(train->value)) {
//            fprintf(stderr, "数据长度超出缓冲区大小\n");
//            return -1;
//        }
//        train->value[train->lenth] = '\0'; // 确保字符串终止
//        printf("Received value: %s\n", train->value);
//    }

     // 先接收type和lenth
    if (recv(sockfd, train, sizeof(int) * 2, 0) <= 0) {
        perror("接收头部失败");
        return -1;
    }

    if (train->lenth > 0) {
        if (train->lenth >= sizeof(train->value)) {
            fprintf(stderr, "数据长度超出缓冲区大小\n");
            return -1;
        }

        if (recv(sockfd, train->value, train->lenth, 0) <= 0) {
            perror("接收数据失败");
            return -1;
        }
        train->value[train->lenth] = '\0';  // 确保字符串正确终止
    }
    return 0;
}


int signAndAuthPasswd(int sockfd, char* username, char* passwd, char *mode) {
    train_t train;

    printf("Mode: %s (login, signup, delete)\n", mode);

    if (strcmp(mode, "delete") == 0) {
        // 实现注册、登录和注销功能
        // mode: 0=登录, 1=注册, 2=注销

        // 处理注销请求
        if (sendTrain(sockfd, DEL, username) < 0) {
            return -1;
        }

        if (receiveTrain(sockfd, &train) < 0) {
            return -1;
        }

        return (strcmp(train.value, "SUCCESS") == 0) ? 0 : -1;
    }

    int reqType = -1;
    if(strcmp(mode, "signup") == 0){
        reqType = SIGN_IN;
    }
    if(strcmp(mode, "login") == 0){
        reqType = AUTH;
    }

    // 发送用户名
    if (sendTrain(sockfd, reqType, username) < 0) {
        return -1;
    }
    printf("send sucess\n");
    // 接收salt值
    if (receiveTrain(sockfd, &train) < 0) {
        return -1;
    }
    
    // 使用crypt加密密码
    char *encrypted = crypt(passwd, train.value);
    if (encrypted == NULL) {
        perror("crypt failed");
        return -1;
    }

    // 发送加密后的密码
    if (sendTrain(sockfd, reqType, encrypted) < 0) {
        return -1;
    }

    // 接收登录/注册结果
    if (receiveTrain(sockfd, &train) < 0) {
        return -1;
    }

    return (strcmp(train.value, "SUCCESS") == 0) ? 0 : -1;
}
